package cn.lingyangwl.agile.pay.client.listener;

import cn.lingyangwl.framework.mq.base.manager.MqManager;
import cn.lingyangwl.framework.tool.core.exception.BizException;
import cn.lingyangwl.agile.common.core.exception.enums.GlobalErrorEnum;
import cn.lingyangwl.agile.common.core.model.EventMessage;
import com.alibaba.fastjson2.JSON;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author shenguangyang
 */
@Slf4j
@Lazy(value = false)
@Component
//@RabbitListener(bindings = {
//        @QueueBinding(
//                value = @Queue(value = MqCons.ORDER_ERROR_QUEUE, durable = "true"), key = MqCons.ORDER_ERROR_KEY,
//                exchange = @Exchange(value = MqCons.ORDER_ERROR_EXCHANGE, type = ExchangeTypes.TOPIC))
//})
public class GoodsOrderErrorListener {
    @Resource
    private MqManager mqManager;

    @RabbitHandler
    public void onMessage(EventMessage eventMessage, Channel channel, Message message) {
        try {
            // 判断是否重复消费
            if (mqManager.isConsumed(eventMessage.getMsgId())) {
                log.warn("message {} are repeatedly consumed, do not perform business", eventMessage.getMsgId());
                return;
            }
            mqManager.markConsumed(eventMessage.getMsgId());

            log.info("消费成功: {}", JSON.toJSONString(eventMessage));
            log.info("告警发送短信成功...");
        } catch (Exception e) {
            log.error("消费失败: {}", e.getMessage());
            throw new BizException(GlobalErrorEnum.MQ_LISTENER_ERROR);
        }
    }
}
